{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## クラス継承とポリモーフィズム(polymorphism)\n",
    "\n",
    "* クラス継承\n",
    "    * 既存のクラスのフィールドやメソッドを利用して新しいクラスを楽に作る仕組み\n",
    "* ポリモーフィズム\n",
    "    * 多様性、多態性\n",
    "    * 類似したクラスのメソッドの呼び出し方を共通にする仕組み"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Person\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class ${name} {\n",
    "  String name;\n",
    "  int age;\n",
    "  String address;\n",
    "  \n",
    "  Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  void say(){\n",
    "    System.out.println(\"Name:\" + name + \" Age:\" + age + \" Address:\" + address);\n",
    "  }\n",
    "  \n",
    "  void setName(String _name){\n",
    "    name = _name;\n",
    "  }\n",
    "  \n",
    "  void setAge(int _age){\n",
    "    if (_age > 0) {\n",
    "      age = _age;\n",
    "    }else{\n",
    "      age = 0;\n",
    "    }\n",
    "  }\n",
    "  \n",
    "  void setAddress(String _address){\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  String getName(){\n",
    "    return name;\n",
    "  }\n",
    "  \n",
    "  int getAge(){\n",
    "    return age;\n",
    "  }\n",
    "  \n",
    "  String getAddress(){\n",
    "    return address;\n",
    "  }\n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Boy\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class ${name} extends Person{\n",
    "  \n",
    "  //# superを利用してスーパークラスのフィールドを利用してコンストラクタを簡単に書いている\n",
    "  Boy(String _name, int _age, String _address){\n",
    "    super(_name, _age, _address);\n",
    "  }\n",
    "  \n",
    "  void say(){\n",
    "    System.out.println(\"Boy Name:\" + name + \" Boy Age:\" + age + \" Boy Address:\" + address);\n",
    "  }  \n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=Girl\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class ${name} extends Person{\n",
    "  \n",
    "  Girl(String _name, int _age, String _address){\n",
    "    super(_name, _age, _address);\n",
    "  }\n",
    "  \n",
    "  void say(){\n",
    "    System.out.println(\"Girl Name:\" + name + \" Girl Age:\" + age + \" Girl Address:\" + address);\n",
    "  }  \n",
    "}\n",
    "EOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n",
      "Boy Name:Jiro Boy Age:19 Boy Address:Tokushima\n",
      "Girl Name:Yoshiko Girl Age:20 Girl Address:Toyama\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "    Person[] people = new Person[4];\n",
    "    people[0] = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    people[1] = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "    //# スーパークラス型(Person型)の変数でサブクラスのインタンスを指す\n",
    "    people[2] = new Boy(\"Jiro\", 19, \"Tokushima\");\n",
    "    people[3] = new Girl(\"Yoshiko\", 20, \"Toyama\");\n",
    "\n",
    "    for(int i = 0; i < people.length; i++){\n",
    "      people[i].say();\n",
    "    }\n",
    "\n",
    "    //# for(int i = 0; i < people.length; i++){\n",
    "     //#  System.out.println(people[i].getName() + \" \" + people[i].getAge() + \" \" + people[i].getAddress());\n",
    "    //# }\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* アクセス修飾子\n",
    "    * Person, Boy, Girlのアクセス修飾子は指定なしなので同一クラス、同一パッケージからアクセス可能\n",
    "    * フィールドやメソッドを指定なしにしたのはprivateにするとサブクラスが継承できないため\n",
    "        * <font color=\"red\">クラスを継承させたい場合、スーパークラスのアクセス修飾子は指定なしかprotectedにする必要がある</font>\n",
    "* クラスの継承\n",
    "    * extends節を使う\n",
    "        * extends節の後ろに継承したいクラス名を書く\n",
    "    * 今回の例\n",
    "        * Personクラスを継承してBoy, Girlクラスを作成している\n",
    "            * Personがスーパークラス、Boy,Girlが<font color=\"red\">サブクラス</font>\n",
    "    * 継承してできること、できないこと\n",
    "        * スーパークラスで定義したフィールドやメソッドをサブクラスでは定義すること無く利用できる\n",
    "            * Boy,Girlクラスではフィールドを宣言していないが使えている\n",
    "        * サブクラス側で再定義\n",
    "            * サブクラスで定義されたものが優先される\n",
    "                * フィールドの隠蔽\n",
    "                    * この例では無し\n",
    "                * メソッドのオーバーライド\n",
    "                    * say()メソッドをオーバーライドしている\n",
    "                    * インスタンスごとに表示を変えている\n",
    "        * <font color=\"red\">コンストラクタは継承されない</font>\n",
    "            * サブクラス側で再定義しなければならない\n",
    "            * superを使えば1つ上のスーパークラスのフィールドやメソッドにアクセスできるのでサブクラスのコンストラクタは簡単に書ける\n",
    "* 同型の変数\n",
    "    * クラスのインスタンスは同型の変数で指し示すことができる\n",
    "        * Personのインスタンスを指すにはPerson型の変数しか使えない\n",
    "    *クラスが継承関係にある場合、スーパークラス型の変数でサブクラスのインスタンスも指すことができる\n",
    "        * ただし、<font color=\"red\">スーパークラスにはないサブクラス独自のフィールドやメソッドはスーパークラス型の変数は利用できない</font>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
